<template>
  <div class="addBills">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="月份" prop="time">
        <el-date-picker
          v-model="ruleForm.time"
          value-format="yyyy-MM"
          type="month"
          placeholder="选择月份"
        />
      </el-form-item>

      <el-form-item label="金额" prop="money">
        <el-input v-model="ruleForm.money" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import billsApi from '../../api/bills'

export default {
  data() {
    return {
      ruleForm: {
        time: '',
        money: ''
      },
      rules: {
        money: [{ required: true, message: '请输入金额', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async submitForm() {
      const data = await billsApi.postOrder(this.ruleForm) // 生成缴费订单
      if (data) {
        this.$message({
          message: '开通缴费成功',
          type: 'success'
        })
        this.ruleForm.time = ''
        this.ruleForm.money = ''
      } else {
        this.$message.error('当前月已开通缴费')
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.addBills {
  margin-top: 30px;
  width: 320px;
}
</style>
